<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\AngerbirdServices;

//愤怒的小鸟
class ApiAngerbirdController extends BaseController
{
    protected $angerbirdService;

    public function __construct(){
        $this->angerbirdService = new AngerbirdServices();
    }

    /**
     * 测试
     */
    public function angerbirdTest(Request $request)
    {

    }

    /**
     * 愤怒的小鸟游戏配置获取
     */
    public function angerbirdGetConfig(Request $request)
    {
        $userId = $this->getLoginUserId();

        $data = $this->angerbirdService->angerbirdGetConfig($userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟兑换商店列表
     */
    public function angerbirdGetShopList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->angerbirdService->angerbirdGetShopList($userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟兑换商店兑换
     */
    public function angerbirdShopBuy(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $shopId = (int) $request->input('shop_id', 0);
        $giftNum = (int) $request->input('gift_num', 1);
        if(empty($shopId) || empty($giftNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->angerbirdService->angerbirdShopBuy($userId, $shopId, $giftNum);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟兑换商店记录列表
     */
    public function angerbirdGetShopLog(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $limit = (int) $request->input('page_size', 10);
        $page = (int) $request->input('page_index', 0);

        $data = $this->angerbirdService->angerbirdGetShopLog($userId, $page, $limit);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟购买能量
     */
    public function angerbirdBuyEnergy(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $num = (int) $request->input('num', 0);
        $num = intval($num);
        if($num <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->angerbirdService->angerbirdBuyEnergy($userId, $num);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟用户初始押注
     */
    public function angerbirdUserFirstStake(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $height = (int) $request->input('height', 0);
        $energyNum = (int) $request->input('energy_num', 0);
        $gameNum = (int) $request->input('game_num', 0);
        if(($height<100) || ($energyNum<100) || empty($gameNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $key = 'ANGERBIRD:userStakeDo:'.$userId;
        if (!redis_lock($key, 60)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '处理中, 同一时间请勿重复请求');
        }
        $data = $this->angerbirdService->angerbirdUserFirstStake($userId, $height, $energyNum, $gameNum);
        redis_unlock($key);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟用户叠加押注
     */
    public function angerbirdUserStake(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $energyNum = (int) $request->input('energy_num', 0);
        $gameNum = (int) $request->input('game_num', 0);
        if(($energyNum<10) || empty($gameNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $key = 'ANGERBIRD:userStakeDo:'.$userId;
        if (!redis_lock($key, 60)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '处理中, 同一时间请勿重复请求');
        }
        $data = $this->angerbirdService->angerbirdUserStake($userId, $energyNum, $gameNum);
        redis_unlock($key);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟用户撤销押注
     */
    public function angerbirdUserStakeBack(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $gameNum = (int) $request->input('game_num', 0);
        if(empty($gameNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $key = 'ANGERBIRD:userStakeDo:'.$userId;
        if (!redis_lock($key, 60)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '处理中, 同一时间请勿重复请求');
        }
        $data = $this->angerbirdService->angerbirdUserStakeBack($userId, $gameNum);
        redis_unlock($key);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟用户兑换能量币
     */
    public function angerbirdUserReward(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $gameNum = (int) $request->input('game_num', 0);
        $height = (int) $request->input('height', 0);
        if(empty($gameNum) || empty($height)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $key = 'ANGERBIRD:userStakeDo:'.$userId;
        if (!redis_lock($key, 60)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '处理中, 同一时间请勿重复请求');
        }
        $data = $this->angerbirdService->angerbirdUserReward($userId, $gameNum, $height);
        redis_unlock($key);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟本局押注用户列表
     */
    public function angerbirdUserList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->angerbirdService->angerbirdUserList($userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟结果记录
     */
    public function angerbirdResList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->angerbirdService->angerbirdResList();

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 愤怒的小鸟用户飞行记录
     */
    public function angerbirdUserResList(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $limit = (int) $request->input('page_size', 50);
        $page = (int) $request->input('page_index', 0);

        $data = $this->angerbirdService->angerbirdUserResList($userId, $page, $limit);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }



}
